package controller;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pojo.Resat;
import pojo.Tb_topic;
import dao.BaseDao;
import service.ResatService;
import service.TopicService;

@Controller
public class KaoshiController {
	@Resource
	private TopicService tps;
	@Resource
	private ResatService rs;
	@Resource
	private BaseDao<Tb_topic>bd;
	@RequestMapping(value="/akaoshi",method=RequestMethod.GET)
	public String kaoshi(@RequestParam("kemu")String kemu,Model m,HttpSession session){
		int k = 0;
		if(kemu.equals("yw"))k=1;
		else if(kemu.equals("sx"))k=1001;
		else if(kemu.equals("jz"))k=1001;
		else if(kemu.equals("null")) return "/admin/aindex";
		Random rd=new Random();
		session.setAttribute("subid", k);
		List<Tb_topic>list=tps.findbysub(k);
		Tb_topic topic;
		List<Tb_topic>cache=new LinkedList<Tb_topic>();
		int a=0,b=0;
		while(cache.size()<50)
		{	
			a=rd.nextInt(10);
			b=rd.nextInt(10);
			while(a<list.size()&&cache.size()<50){
				topic=list.get(a);
				cache.add(topic);
				a=a+b;			
			}
		}
		m.addAttribute("list", cache);
		return "/admin/kaoshi";
	}
	@RequestMapping(value="/jilu",method=RequestMethod.GET)
	public String ksjilu(Model m){
		List<Resat> list=rs.findall();
		m.addAttribute("jilu", list);
		return "/admin/ksjl";
	}
	@RequestMapping(value="/xiewanjiaojuan" ,method=RequestMethod.POST)
	public String jiaojuan(HttpServletRequest request, HttpServletResponse response,HttpSession session,Model m){
		 Calendar cal = Calendar.getInstance();
	        int year = cal.get(Calendar.YEAR);//获取年份
	        int month=cal.get(Calendar.MONTH);//获取月份
	        int day=cal.get(Calendar.DATE);//获取日
	        int hour=cal.get(Calendar.HOUR);//小时 
	        int min=cal.get(Calendar.MINUTE);
	        int sec=cal.get(Calendar.SECOND);
	        String user=session.getAttribute("user").toString();
	        String sbid="";
	        sbid= session.getAttribute("subid").toString();
	        int sudid=Integer.valueOf(sbid.trim());
	     String ksid=""+year+month+day+hour+min+sec;   
	    
	     	int defen=0;
	     for(int i=0;i<50;i++){
	    	 int id=0;
	    	 if(request.getParameter("id"+i)!=null)
	    	 id=Integer.valueOf( request.getParameter("id"+i).trim());
	    	Tb_topic tp=tps.findbyid(id);
	    	String ans=tp.getAnswer();
	    	String chouse="";
	    	if(request.getParameter("ob"+i)!=null)
	    	chouse=request.getParameter("ob"+i);
	    	if(ans.equals(chouse))defen=defen+2;
	     }
	     Resat r=new Resat();
	     r.setDefen(defen);
	     r.setKs_id(ksid);
	     r.setStu_name(user);
	     r.setSub_id(sudid);
	     rs.insert(r);
	     m.addAttribute("zongfen", defen);
		return "/admin/defeng";
	}
}
